package one.stand.service;

import com.alibaba.fastjson.JSON;
import one.stand.OneCrmServiceApplicationTests;
import one.stand.code.RedisCode;
import one.stand.entity.TokenEntity;
import one.stand.request.CompanyRequest;
import one.stand.request.CompanyVideoRequest;
import one.stand.util.RedisUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@TestConfiguration
public class Company extends OneCrmServiceApplicationTests {
    @Autowired
    CompanyService companyService;
    @Autowired
    protected RedisUtil redisUtil;

    @Test
    public void getSurplusDeliveryTime(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("现在的日期是：" + format.format(date));
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE, -3);
        ca.add(Calendar.HOUR, -12);
        date = ca.getTime();
        System.out.println("增加天数以后的日期：" + format.format(date));
        System.err.println(getSurplusDeliveryTime(date));
    }

    public String getSurplusDeliveryTime(Date deliveryTime) {
        if (null == deliveryTime) {
            return "";
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(deliveryTime);
        ca.add(Calendar.DATE, 15);
        Date date = ca.getTime();

        Date now = new Date();

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = date.getTime() - now.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    @Test
    public void getLoginInfo() {
        String token = "fksdjfdskjdsf_17";
        System.err.println(JSON.toJSONString(redisUtil.get(RedisCode.getUserLoginToken(token))));
        TokenEntity tokenModel = new TokenEntity();
        tokenModel.setRoleId(13523);
        redisUtil.set(RedisCode.getUserLoginToken(token), tokenModel, 7000, TimeUnit.SECONDS);
        System.err.println(JSON.toJSONString(redisUtil.get(RedisCode.getUserLoginToken(token))));
    }

    @Test
    public void moveCompanyVideo() {
        CompanyVideoRequest request = new CompanyVideoRequest();
        request.setCId(67L);
        request.setVideoId(2);
        request.setType(2);
        System.err.println(JSON.toJSON(companyService.moveCompanyVideo(request)));
    }

    @Test
    public void add() {
        CompanyRequest request = new CompanyRequest();
        request.setId(1L);
        request.setCompanyLogo("123.jpg");
        request.setCompanyName("壹立科技");
        request.setCompanyNotice("壹立第一");
        request.setIndustryId(1);
        System.err.println(JSON.toJSON(companyService.addCompany(request)));
    }

    @Test
    public void getCompanyCard() {
        CompanyRequest request = new CompanyRequest();
        request.setCId(1L);
        request.setPageNum(1);
        request.setPageSize(10);
    }

    @Test
    public void invitation() {

        System.err.println(JSON.toJSON(companyService.invitation(new CompanyRequest() {{
            setCompanyId(3);
        }})));
    }

    @Test
    public void upd() {
        CompanyRequest request = new CompanyRequest();
        request.setCompanyId(3);
        request.setCompanyLogo("123123123.jpg");
        request.setCompanyName("壹立科技");
        request.setCompanyNotice("壹立天下第一");
        request.setIndustryId(1);
        System.err.println(JSON.toJSON(companyService.updCompany(request)));
    }

    @Test
    public void getCompanyAllInfo(){
        System.err.println(JSON.toJSON(companyService.getCompanyAllInfo(new CompanyRequest(){{
        }})));
    }
    @Test
    public void keys(){
        TokenEntity tokenEntity=redisUtil.get(RedisCode.getUserLoginToken("123"));
        System.err.println("----"+tokenEntity.toString());
        System.err.println(redisUtil.getKey(RedisCode.getUserLoginToken("*_17")));
    }
}
